package com.hx.myapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import util.ToastUtil;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener {
   private Button mBtnDialog1,mBtnDialog2,mBtnDialog3,mBtnDialog4,mBtnDialog5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        mBtnDialog1 = findViewById(R.id.btn_dialog1);
        mBtnDialog2 = findViewById(R.id.btn_dialog2);
        mBtnDialog3 = findViewById(R.id.btn_dialog3);
        mBtnDialog4 = findViewById(R.id.btn_dialog4);
        mBtnDialog5 = findViewById(R.id.btn_dialog5);
        mBtnDialog1.setOnClickListener(this);
        mBtnDialog2.setOnClickListener(this);
        mBtnDialog3.setOnClickListener(this);
        mBtnDialog4.setOnClickListener(this);
        mBtnDialog5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_dialog1:
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("请回答").setMessage("你觉得课程如何？")
                        .setIcon(R.drawable.icon_user)
                        .setPositiveButton("棒", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ToastUtil.showMag(DialogActivity.this,"你很诚实");
                            }
                        }).setNeutralButton("还行", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtil.showMag(DialogActivity.this,"你再瞅瞅");
                    }
                }).setNegativeButton("不好", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtil.showMag(DialogActivity.this,"睁眼说瞎话");
                    }
                }).show();
                 break;
            case R.id.btn_dialog2:
                final String[] array = new String[]{"男","女"};
                AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                builder1.setTitle("选择性别").setItems(array, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtil.showMag( DialogActivity.this,array[which]);
                    }
                }).show();
                break;
            case R.id.btn_dialog3:
                final String[] array2 = new String[]{"男","女"};
                AlertDialog.Builder builder2 = new AlertDialog.Builder(DialogActivity.this);
                builder2.setSingleChoiceItems(array2, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtil.showMag( DialogActivity.this,array2[which]);
                        dialog.dismiss();
                    }
                }).setTitle("请选择性别").setCancelable(false)
                  .show();
                break;
            case R.id.btn_dialog4:
                final String[] array3 = new String[]{"唱歌","跳舞","写代码"};
                boolean[] isSelected = new boolean[]{false,false,true};
                AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
                builder3.setTitle("请选择兴趣：").setMultiChoiceItems(array3, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        ToastUtil.showMag( DialogActivity.this,array3[which] + ":" + isChecked);
                       // dialog.dismiss();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setCancelable(false).show();
                break;
            case R.id.btn_dialog5:
                AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
               View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
               EditText etUserName = view.findViewById(R.id.et_username);
               EditText etPassWord = view.findViewById(R.id.et_password);
               Button btnLogin = view.findViewById(R.id.btn_login);
               builder4.setTitle("请先登录").setView(view).setCancelable(false);
               final AlertDialog dialog =  builder4.show();
                btnLogin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtil.showMag( DialogActivity.this,etUserName.getText().toString()+":" +etPassWord.getText().toString());
                        dialog.dismiss();
                    }
                });
                break;
        }
    }
}